﻿//Write methods that calculate the surface of a triangle by given:
//  Side and an altitude to it;
//  Three sides;
//  Two sides and an angle between them.
//Use System.Math.


using System;

public class TriangleSurface
{
    public static void Main(string[] args)
    {
        Console.WriteLine("1.Side and an altitude to it.");
        Console.WriteLine("2.Three sides.");
        Console.WriteLine("3.Two sides and an angle between them.");
        Console.WriteLine();
        Console.WriteLine("Choose metod between \'1\' and \'3\' to calculate surface of triangle");
        double surface = 0;
        bool loop = true;
        while (loop)
        {
            surface = GetUserChoice(surface);
            if (surface != 0)
            {
                Console.WriteLine("Surface of triangle is {0}", surface);
                loop = false;
            }
            else
            {
                Console.WriteLine("Bad data input! Try again! ");
            }
        }
    }

    private static double GetUserChoice(double surface)
    {
        try
        {
            int choice = int.Parse(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    {
                        Console.WriteLine("Enter positive number for side of triangle");
                        double sideOfTriangle = double.Parse(Console.ReadLine());
                        Console.WriteLine("Enter positive number for altitude to side of triangle");
                        double altitude = double.Parse(Console.ReadLine());
                        surface = CalculateTriangleSurface(sideOfTriangle, altitude);
                    }

                    break;
                case 2:
                    {
                        Console.WriteLine("Enter positive number for first side of triangle");
                        double firstSide = double.Parse(Console.ReadLine());
                        Console.WriteLine("Enter positive number for second side of triangle");
                        double secondSide = double.Parse(Console.ReadLine());
                        Console.WriteLine("Enter positive number for third side of triangle");
                        double thirdSide = double.Parse(Console.ReadLine());
                        surface = CalculateTriangleSurface(firstSide, secondSide, thirdSide);
                    }

                    break;
                case 3:
                    {
                        Console.WriteLine("Enter positive number for first side of triangle");
                        double firstSide = double.Parse(Console.ReadLine());
                        Console.WriteLine("Enter positive number for second side of triangle");
                        double secondSide = double.Parse(Console.ReadLine());
                        Console.WriteLine("Enter positive number for angle between sides of triangle");
                        double angle = double.Parse(Console.ReadLine());
                        surface = CalculateTriangleSurface(firstSide, secondSide, angle);
                    }

                    break;
                default: throw new ArgumentOutOfRangeException();
            }
        }
        catch (FormatException)
        {
            Console.WriteLine("Invalid number");
            surface = 0;
        }
        catch (ArgumentOutOfRangeException)
        {
            Console.WriteLine("Choice mast be 1 or 2 or 3");
            surface = 0;
        }

        return surface;
    }

    private static double CalculateTriangleSurface(double sideOfTriangle, double altitude)
    {
        double surface = (sideOfTriangle * altitude) / 2;
        return surface;
    }

    private static double CalculateTriangleSurface(double firstSide, double secondSide, double thirdSide, double halfPerimeter = 0)
    {
        halfPerimeter = (firstSide + secondSide + thirdSide) / 2;
        double surface = Math.Sqrt(halfPerimeter * (halfPerimeter - firstSide) * (halfPerimeter - secondSide) * (halfPerimeter - thirdSide));
        return surface;
    }

    private static double CalculateTriangleSurface(double firstSide, double secondSide, double angle)
    {
        double surface = (firstSide * secondSide * Math.Sin(angle * (Math.PI / 180))) / 2;
        return surface;
    }
}